package com.xinchao.ck_mips.base;

import com.xinchao.rxtools.rxhttp.tools.IDisposable;

import io.reactivex.subscribers.DisposableSubscriber;

/**
 * Created by wq on 2019/5/7.
 */
public abstract class CKSubscriber<T> extends DisposableSubscriber<T> {
    private IDisposable mIDisposable;

    public CKSubscriber(IDisposable disposable) {
        this.mIDisposable = disposable;
    }

    @Override
    public void onNext(T t) {
        next(t);
    }

    protected abstract void next(T t);

    @Override
    public void onError(Throwable t) {
        finish();
    }

    @Override
    public void onComplete() {
        finish();
    }

    protected void finish() {
        if (mIDisposable != null) {
            mIDisposable.removeDisposable(this);
        } else {
            dispose();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mIDisposable != null) {
            mIDisposable.addDisposable(this);
        }
    }
}
